﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using log4net;
using AutoFacUtils;
using CashFlow.Db.Entity;
using CashFlow.ShopManage.Service;
using CashFlow.ShopManage.Filters;
using CashFlow.ShopManage.Models.Fee;
using CashFlow.ShopManage.Validator.Fee;
using CashFlow.ShopManage.Common;

namespace CashFlow.ShopManage.Controllers
{
    public class FeeController : Controller
    {
        private static ILog _logger = LogManager.GetLogger(typeof(FeeController));
        private IAdminService GetAdminService() { return AutoFacHelper.Get<IAdminService>(); }
        private IFeeService GetFeeService() { return AutoFacHelper.Get<IFeeService>(); }

        // GET: Fee
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ValidatorFilter(typeof(QueryFeeModelValidator))]
        public ActionResult List(QueryFeeModel model)
        {
            var adminService = GetAdminService();
            var ui = adminService .GetLoginUserInfo();
            var feeService = GetFeeService();
            var ls = feeService.QueryByShopManagerUserId(ui.ShopManagerUserId
                , model.Date
                , model.Name);
            var ls2 = ls.Select(x => Convert(x)).ToList();
            return MyCommonTool.CreateLayUITableResult(true, "", ls2, ls2.Count);
        }

        private QueryFeeModelResult Convert(FeeAndUserInfoView model)
        {
            return new QueryFeeModelResult
            {
                Name = model.UserInfo.Name,
                Phone = model.UserInfo.Phone,
                Date = model.Fee.Date,
                OrderProfitFee = model.Fee.OrderProfitFee,
                ShopManagerFee = model.Fee.ShopManagerManageFee,
                BoundFee = model.Fee.BoundFee,
                ConsignmentSaleFee = model.Fee.ConsignmentSaleFee
            };
        }
    }
}